layui.use(['jquery','element','form','upload'],function () {
    var $ = layui.$
        ,element = layui.element
        ,form = layui.form,
        upload = layui.upload;

    var beneficiaryId = getUrlParms("beneficiaryId");
    //校验登录状态
    if(isNotLogIn()){
        return;
    }
    /**
     * 获取乡镇信息
     * @param villageId
     */
    var index = layer.load(3);
    $.ajax({
        url : "/CivilAffairs/manager/getVillageInfo",
        type : "post",
        data : {"villageId":"","isManager":true},
        async : false,
        dataType : "json",
        success : function(result) {
            layer.close(index);
            if(checkResult(result)){
                $(".village-div").html("<label class=\"layui-form-label\">所属乡镇</label>"+result.data);
            }
            form.render('select');
        },
        error:function(result){
            layer.close(index);
            reqError(result,"获取乡镇信息异常");
        }
    });

    //如果是编辑页面,从后台获取新闻的内容
    if(!isNull(beneficiaryId)){
        var index = layer.load(3);
        $.ajax({
            url:"/CivilAffairs/manager/getDataInfoById",
            type:"post",
            data:{"dataId":beneficiaryId,"dataType":"beneficiary"},
            dataType:"json",
            success:function(result){
                layer.close(index);
                if(checkResult(result)){
                    var beneficiaryInfo = result.data[0];
                    $("#beneficiaryName").val(beneficiaryInfo.beneficiary_name);
                    $("#certNum").val(beneficiaryInfo.cert_num);
                    $("#beneficiaryType").val(beneficiaryInfo.beneficiary_type);
                    $("#standards").val(beneficiaryInfo.standards);
                    //失效的新闻展示为未生效
                    if("0"==beneficiaryInfo.valid_tag){
                        var isValid = $("#isValid");
                        isValid.removeAttr("checked");
                    }
                    $("#villages").val(beneficiaryInfo.village_id);
                    form.render();
                    //给提交按钮赋值新闻的itemid
                    $("#addBeneficiary").attr("itemid",beneficiaryInfo.beneficiary_id);
                }
            },
            error : function(result) {
                layer.close(index);
                console.log("获取受惠信息异常:"+result.status+","+result.statusMessage+",beneficiaryId:"+beneficiaryId);
                layer.msg("系统异常,请稍后重试",{icon:2});
            }
        });
    }//显示批量添加功能
    else{
        var addNewPicHtml = "<label class=\"layui-form-label\">批量导入</label><div class=\"layui-input-block\"><div class=\"layui-upload layui-inline\">\n" +
            "\t\t\t\t<a type=\"button\" name=\"image\" class=\"layui-btn layui-btn-normal\" id=\"chooseFile\">选择excel</a>\n" +
            "\t\t\t</div>\n" +
            "\t\t\t<div class=\"layui-upload layui-inline\" style=\"width: 80px\">\n" +
            "\t\t\t\t<a type=\"button\" class=\"layui-btn layui-btn-normal\" id=\"uploadFile\">批量导入</a>\n" +
            "\t\t\t</div></div>";
        $(".upload-div").html(addNewPicHtml);
        var loadIndex;
        upload.render({
            elem: '#chooseFile'
            ,url: '/CivilAffairs/manager/uploadFile'
            ,auto: false
            //,multiple: true//是否允许多文件上传
            ,drag:true //接受拖拽上传
            ,data:{
                optManagerId:function () {
                    //校验登录状态
                    if(isNotLogIn()){
                        return "";
                    }
                    return getCookie("managerId");
                }}
            ,accept:'file' //上传文件类型
            ,exts:'xls|xlsx' //上传文件后缀
            ,field:'file' //后台接收默认字段名
            ,bindAction: '#uploadFile'
            ,before:function () {
                loadIndex = layer.load(2);
            }
            ,done: function(result){ //done是单个上传完执行回调,如果是多图片上传,要使用Alldone来执行回调
                layer.close(loadIndex);
                if(checkResult(result)){
                    layer.msg(result.detail,{icon:6});
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }
                console.log(result)
            }
        });
        form.render();
    }

    //添加&修改新闻的提交
    form.on("submit(addBeneficiary)",function(data){
        //校验登录状态
        if(isNotLogIn()){
            return false;
        }
        var beneficiaryId = $(this).attr("itemid");
        var url = "";
        var reqData = "";
        var managerId = getCookie("managerId");
        //是添加新闻
        if(isNull(beneficiaryId)){
            beneficiaryId = "";
            url = "/CivilAffairs/manager/addBeneficiary";
            reqData = {"beneficiaryInfo":JSON.stringify(data.field),"managerId":managerId};
        }else{
            url = "/CivilAffairs/manager/modifyBeneficiary";
            reqData = {"beneficiaryInfo":JSON.stringify(data.field),"beneficiaryId":beneficiaryId,"managerId":managerId};
        }
        //提交修改
        var index = layer.load(3);
        $.ajax({
            url:url,
            type:"post",
            data:reqData,
            dataType:"json",
            success:function(result){
                layer.close(index);
                if(checkResult(result)){
                    layer.msg(result.detail,{icon:6});
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }
            },
            error : function(result) {
                layer.close(index);
                console.log("获取新闻内容异常:"+result.status+","+result.statusMessage+",beneficiaryId:"+beneficiaryId);
                layer.msg("系统异常,请稍后重试",{icon:2});
            }
        });
        return false;
    });


});